﻿using System;
using UnityEngine;

namespace UniRx
{
	// Token: 0x02000404 RID: 1028
	public abstract class PresenterBase<T> : MonoBehaviour, IPresenter
	{
		// Token: 0x17000188 RID: 392
		// (get) Token: 0x060014F6 RID: 5366 RVA: 0x0005E53E File Offset: 0x0005C93E
		public IPresenter Parent
		{
			get
			{
				return this.parent;
			}
		}

		// Token: 0x060014F7 RID: 5367 RVA: 0x0005E548 File Offset: 0x0005C948
		public IObservable<Unit> InitializeAsObservable()
		{
			if (this.isInitialized)
			{
				return Observable.Return(Unit.Default);
			}
			Subject<Unit> result;
			if ((result = this.initializeSubject) == null)
			{
				result = (this.initializeSubject = new Subject<Unit>());
			}
			return result;
		}

		// Token: 0x060014F8 RID: 5368 RVA: 0x0005E586 File Offset: 0x0005C986
		public void PropagateArgument(T argument)
		{
			this.argument = argument;
		}

		// Token: 0x17000189 RID: 393
		// (get) Token: 0x060014F9 RID: 5369
		protected abstract IPresenter[] Children { get; }

		// Token: 0x060014FA RID: 5370
		protected abstract void BeforeInitialize(T argument);

		// Token: 0x060014FB RID: 5371
		protected abstract void Initialize(T argument);

		// Token: 0x060014FC RID: 5372 RVA: 0x0005E58F File Offset: 0x0005C98F
		public virtual void ForceInitialize(T argument)
		{
			this.Awake();
			this.PropagateArgument(argument);
			this.Start();
		}

		// Token: 0x060014FD RID: 5373 RVA: 0x0005E5A4 File Offset: 0x0005C9A4
		void IPresenter.ForceInitialize(object argument)
		{
			this.ForceInitialize((T)((object)argument));
		}

		// Token: 0x060014FE RID: 5374 RVA: 0x0005E5B4 File Offset: 0x0005C9B4
		void IPresenter.Awake()
		{
			if (this.isAwaken)
			{
				return;
			}
			this.isAwaken = true;
			this.children = this.Children;
			this.childrenCount = this.children.Length;
			for (int i = 0; i < this.children.Length; i++)
			{
				IPresenter presenter = this.children[i];
				presenter.RegisterParent(this);
				presenter.Awake();
			}
			this.OnAwake();
		}

		// Token: 0x060014FF RID: 5375 RVA: 0x0005E623 File Offset: 0x0005CA23
		protected void Awake()
		{
			((IPresenter)this).Awake();
		}

		// Token: 0x06001500 RID: 5376 RVA: 0x0005E62B File Offset: 0x0005CA2B
		protected virtual void OnAwake()
		{
		}

		// Token: 0x06001501 RID: 5377 RVA: 0x0005E630 File Offset: 0x0005CA30
		protected void Start()
		{
			if (this.isStartedCapturePhase)
			{
				return;
			}
			IPresenter presenter = this.parent;
			if (presenter == null)
			{
				presenter = this;
			}
			else
			{
				while (presenter.Parent != null)
				{
					presenter = presenter.Parent;
				}
			}
			presenter.StartCapturePhase();
		}

		// Token: 0x06001502 RID: 5378 RVA: 0x0005E67C File Offset: 0x0005CA7C
		void IPresenter.StartCapturePhase()
		{
			this.isStartedCapturePhase = true;
			this.BeforeInitialize(this.argument);
			for (int i = 0; i < this.children.Length; i++)
			{
				IPresenter presenter = this.children[i];
				presenter.StartCapturePhase();
			}
			if (this.children.Length == 0)
			{
				this.Initialize(this.argument);
				this.isInitialized = true;
				if (this.initializeSubject != null)
				{
					this.initializeSubject.OnNext(Unit.Default);
					this.initializeSubject.OnCompleted();
				}
				if (this.parent != null)
				{
					this.parent.InitializeCore();
				}
			}
		}

		// Token: 0x06001503 RID: 5379 RVA: 0x0005E720 File Offset: 0x0005CB20
		void IPresenter.RegisterParent(IPresenter parent)
		{
			if (this.parent != null)
			{
				throw new InvalidOperationException("PresenterBase can't register multiple parent. Name:" + base.name);
			}
			this.parent = parent;
		}

		// Token: 0x06001504 RID: 5380 RVA: 0x0005E74C File Offset: 0x0005CB4C
		void IPresenter.InitializeCore()
		{
			this.currentCalledCount++;
			if (this.childrenCount == this.currentCalledCount)
			{
				this.Initialize(this.argument);
				this.isInitialized = true;
				if (this.initializeSubject != null)
				{
					this.initializeSubject.OnNext(Unit.Default);
					this.initializeSubject.OnCompleted();
				}
				if (this.parent != null)
				{
					this.parent.InitializeCore();
				}
			}
		}



		// Token: 0x04000C30 RID: 3120
		protected static readonly IPresenter[] EmptyChildren = new IPresenter[0];

		// Token: 0x04000C31 RID: 3121
		private int childrenCount;

		// Token: 0x04000C32 RID: 3122
		private int currentCalledCount;

		// Token: 0x04000C33 RID: 3123
		private bool isAwaken;

		// Token: 0x04000C34 RID: 3124
		private bool isInitialized;

		// Token: 0x04000C35 RID: 3125
		private bool isStartedCapturePhase;

		// Token: 0x04000C36 RID: 3126
		private Subject<Unit> initializeSubject;

		// Token: 0x04000C37 RID: 3127
		private IPresenter[] children;

		// Token: 0x04000C38 RID: 3128
		private IPresenter parent;

		// Token: 0x04000C39 RID: 3129
		private T argument = default(T);
	}
}
